/**
 * Created by shaoting on 16/10/13.
 * 上传界面
 */
import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    ListView,
    Image,
    TouchableOpacity,
    Platform,
    Dimensions,
    AsyncStorage
} from 'react-native';

var {width, height} = Dimensions.get('window'); //屏宽高

var UploadJson = require('../Mine/upload.json')

var UploadView = React.createClass({
    componentDidMount(){
        var token = '';
        try {
            AsyncStorage.getItem(
                'token',
                (error,result)=>{
                    if (error){
                        alert('取值失败:'+error);
                    }else{
                        // alert('取值成功:'+result);
                        this.token = result
                    }
                }
            )
        }catch(error){
            alert('失败'+error);
        }
        let formData = new FormData();
        formData.append("userId", '');
        formData.append("page", '0');
        formData.append("size", '5');
        var fetchOptions = {
            method: 'POST',
            headers: {
                'token': this.token
            },
            body:formData,
        };

        let url = 'http://www.pintasty.cn/user/listupload';
        fetch(url, fetchOptions)
            .then((response) => response.json())
            .then((json) => {
                console.log(json)
                if(json.status == 200){
                    this.setState({
                        dataSource:this.state.dataSource.cloneWithRows(json.data),
                    })
                }else  if (json.status == 301){
                    // alert('没有token')
                }else {
                    alert('访问服务器失败')
                }
            }) .done();
    },
    getDefaultProps(){
        //回调函数
        return{
            selectCellAction:null
        }
    },
    getInitialState(){
        return{
            dataSource:new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2})
        }
    },
    render() {
        return (
            <ListView
                bounces={false}
                style={styles.container}
                dataSource={this.state.dataSource}
                renderRow={this.renderRow}
            />
        );
    },
    renderRow(rowData,sectionID,rowID,hightlightRow){
        return(
            <TouchableOpacity onPress = {()=>this.passParasToDynamic(rowData)}>
                <View>
                    <View style={styles.topViewStyle}>
                        <View style={styles.leftViewStyle}>
                            <View><Image source={{uri:rowData.uploadImage}} style={styles.WineImgStyle}/></View>
                            <View styele={styles.wineInfoView}>
                                <Text style={styles.nameStyle}>{rowData.factoryName}</Text>
                                <Text style={styles.escriptionStyle}>{rowData.productName}</Text>
                                <Image source={{uri:'guoqi'}} style={styles.countryStyle} />
                            </View>
                        </View>
                        <View style={{ flexDirection:'row',position:'absolute',right: 10,top:10}}><Image source={{uri:'star_icon'}} style={{width:15,height:15,marginRight:5}} /><Text>{rowData.compositeScore}</Text></View>
                        <View style={{justifyContent:'center',marginRight: 5}}><Image source={{uri:'return_h_icon'}} style={{width:10,height:15,position:'absolute',right:2}} /></View>
                    </View>

                    <View><Text style={styles.uploadTimeStyle}>上传时间: {rowData.uploadTime}</Text></View>

                    <View style={{backgroundColor:'rgba(234,234,234,1.0)',height:15}}></View>
                </View>
            </TouchableOpacity>
        )
    },
    passParasToDynamic(rowData){
        // console.log(rowData)
        // 判断
        // if (this.props.selectCellAction == null) return;
        this.props.selectCellAction(rowData)
        // 使用global传值方法
        global.uploadProductName = rowData.productName
    }
});


var styles = StyleSheet.create({
    container: {
        width:width,
        backgroundColor: 'white',
    },
    topViewStyle:{
        flexDirection:'row',
        justifyContent:'space-between'
    },
    leftViewStyle:{
        marginTop:10,
        marginLeft:15,
        flexDirection:'row'
    },
    wineInfoView:{

    },
    WineImgStyle: {
        width:100,
        height:120,
        marginRight:5
    },
    nameStyle:{
        color:'cyan'
    },
    escriptionStyle:{
        fontSize:20,
        marginTop:5
    },
    countryStyle:{
        width:20,
        height:20,
        position:'absolute',
        bottom:1
    },
    uploadTimeStyle:{
        marginTop:5,
        marginLeft:15,
        marginBottom:5
    }

});

module.exports = UploadView;